from datetime import datetime

from start import db
# from app.extensions import db

post_tag = db.Table(
    'post_tags',
    db.Column('post_id', db.Integer, db.ForeignKey('posts.id'),primary_key=True),
    db.Column('tag_id', db.Integer, db.ForeignKey('tags.id'),primary_key=True)
)
class BaseModel(db.Model):
    '''基类模型'''
    __abstract__ = True
    id = db.Column(db.Integer, primary_key=True, comment='主键')
    create_time = db.Column(db.DateTime, default=datetime.now(), comment='创建时间')
    # onupdate参数表示更新文章时更新时间
    update_time = db.Column(db.DateTime, default=datetime.now(), onupdate=datetime.now(), comment='更新时间')


class Post(BaseModel):
    '''文章模型'''
    __tablename__ = 'posts'
    title = db.Column(db.String(100), comment='标题')
    content_md = db.Column(db.Text, comment='markdown内容')
    content_html = db.Column(db.Text, comment='html内容')
    read_count = db.Column(db.Integer, default=0, comment='阅读量')
    draft_flag = db.Column(db.Boolean, default=False, comment='是否为草稿')
    # category_id = db.Column(db.Integer, db.ForeignKey('categories.id'), comment='分类id')
    # tages = db.relationship('Tag', secondary=post_tag, backref='posts')
    # comments = db.relationship('Comment', backref='post',cascade='delete')
    # attachments = db.relationship('Attachment', backref='post')
    # user_id = db.Column(db.Integer, db.ForeignKey('users.id'), comment='用户id')

class Category(BaseModel):
    '''分类模型'''
    __tablename__ = 'categories'
    name = db.Column(db.String(100),unique=True, comment='分类名称')
    # posts = db.relationship('Post', backref='category')

class Tag(BaseModel):
    '''标签模型'''
    __tablename__ = 'tags'
    name = db.Column(db.String(100), unique=True, comment='标签名称')
    color = db.Column(db.String(100), comment='分类颜色')
class Note(BaseModel):
    '''笔记模型'''
    __tablename__ = 'notes'
    title = db.Column(db.String(100), comment='标题')
    content_md = db.Column(db.Text, comment='markdown内容')
    content_html = db.Column(db.Text, comment='html内容')
    read_count = db.Column(db.Integer, default=0, comment='阅读量')

class Comment(BaseModel):
    '''评论模型'''
    __tablename__ = 'comments'
    content = db.Column(db.Text, comment='评论内容')
    vistor_ip = db.Column(db.String(100), comment='评论者ip')
    vistor_address = db.Column(db.String(100), comment='评论者地址')
    vistor_email = db.Column(db.String(100), comment='评论者邮箱')
    vistor_name = db.Column(db.String(100), comment='评论者昵称')
    # post_id = db.Column(db.Integer, db.ForeignKey('posts.id'), comment='文章id')
class Message(BaseModel):
    '''留言模型'''
    __tablename__ = 'messages'
    content = db.Column(db.Text, comment='留言内容')
    author_ip = db.Column(db.String(100), comment='留言者ip')
    author_address = db.Column(db.String(100), comment='留言者地址')

class Attachment(BaseModel):
    '''附件模型'''
    __tablename__ = 'attachments'
    file_name = db.Column(db.String(100), comment='文件名')
    file_path = db.Column(db.String(100), comment='文件路径')
    file_size = db.Column(db.Integer, comment='文件大小')